{% extends "head.html" %}
{% block title %}
    vSphere - By Resource Pool
{% endblock %}
{% block content %}
    <div class="alert alert-info">
        <p>Reserved resources by Resource Pool</p>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div id="GuestColumnChart"></div>
            </div>
        </div>
        <div class="col-md-12">
            <div class="panel panel-default">
                <div id="MemoryColumnChart"></div>
            </div>
        </div>
        <div class="col-md-12">
            <div class="panel panel-default">
                <div id="DiskColumnChart"></div>
            </div>
        </div>
        <div class="col-md-12">
            <div class="panel panel-default">
                <div id="VcpuColumnChart"></div>
            </div>
        </div>
    </div>
    <script>

        function round_number(num, dec) {
            return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
        }

        function humanize_filesize(fs) {
            if (fs >= 1099511627776) {
                return round_number(fs / 1099511627776, 2) + ' TB';
            }
            if (fs >= 1073741824) {
                return round_number(fs / 1073741824, 2) + ' GB';
            }
            if (fs >= 1048576) {
                return round_number(fs / 1048576, 2) + ' MB';
            }
            if (fs >= 1024) {
                return round_number(fs / 1024, 0) + ' KB';
            }
            return fs + ' B';
        }

        {% regroup resource_pool|dictsort:"mem_reserved" by mem_reserved as resource_pool_mem %}
        $(function () {
            var chart;
            $(document).ready(function () {
                chart = new Highcharts.Chart({
                    credits: {
                        enabled: false
                    },
                    chart: {
                        renderTo: 'MemoryColumnChart'
                    }, title: {
                        text: 'Reserved Memory'
                    }, yAxis: {
                        title: {
                            text: 'Memory ( B )'
                        }, labels: {
                            formatter: function() {
                                return humanize_filesize(this.value * 1024/1000 * 1048576);
                            }
                        }
	                }, xAxis: {
                        categories: [
                            {% for rp in country_list %}
                                {% for a in rp.list %}
                                    '{{ a.name}}',
                                {% endfor %}
                            {% endfor %}
                            ],
                        labels: {
                            rotation: -45,
                            align: 'right'
                        }
                    }, tooltip: {
                        formatter: function () {
                            return '<b>' + this.point.name + '</b>: ' + humanize_filesize(this.y * 1048576);
                        }
                    }, exporting: {
                        enabled: false
                    }, plotOptions: {
                        column: {
                            dataLabels: {
                                enabled: false
                            }
                        }
                    }, legend: {
                        enabled: false
                    }, series: [
                        {
                            type: 'column',
                            data: [
                                {% for rp in resource_pool_mem %}
                                    {% for a in rp.list %}
                                        [ '{{ a.name}}', {{ a.mem_reserved }} ],
                                    {% endfor %}
                                {% endfor %}
                            ]
                        }
                    ]
                });
            });
        });

        {% regroup resource_pool|dictsort:"disk_reserved" by disk_reserved as resource_pool_disk %}
        $(function () {
            var chart;
            $(document).ready(function () {
                chart = new Highcharts.Chart({
                    credits: {
                        enabled: false
                    },
                    chart: {
                        renderTo: 'DiskColumnChart'
                    }, title: {
                        text: 'Reserved Storage'
                    }, yAxis: {
                        title: {
                            text: 'Space ( B )'
                        }, labels: {
                                formatter: function() {
                                    return humanize_filesize(this.value * 1024/1000 * 1048576);
                                }
                        }
	                 }, xAxis: {
                        categories: [
                            {% for rp in country_list %}
                                {% for a in rp.list %}
                                    '{{ a.name}}',
                                {% endfor %}
                            {% endfor %}
                            ],
                        labels: {
                            rotation: -45,
                            align: 'right'
                        }
                    }, tooltip: {
                        formatter: function () {
                            return '<b>' + this.point.name + '</b>: ' + humanize_filesize(this.y * 1048576);
                        }
                    }, exporting: {
                        enabled: false
                    }, plotOptions: {
                        column: {
                            dataLabels: {
                                enabled: false
                            }
                        }
                    }, legend: {
                        enabled: false
                    }, series: [
                        {
                            type: 'column',
                            data: [
                                {% for rp in resource_pool_disk %}
                                    {% for a in rp.list %}
                                        [ '{{ a.name}}', {{ a.disk_reserved }} ],
                                    {% endfor %}
                                {% endfor %}
                            ]
                        }
                    ]
                });
            });
        });

        {% regroup resource_pool|dictsort:"vcpu_reserved" by vcpu_reserved as resource_pool_vcpu %}
        $(function () {
            var chart;
            $(document).ready(function () {
                chart = new Highcharts.Chart({
                    credits: {
                        enabled: false
                    },
                    chart: {
                        renderTo: 'VcpuColumnChart'
                    }, title: {
                        text: 'Reserved vCpu'
                    }, yAxis: {
                        title: {
                            text: 'vcpu'
                        }
	                 }, xAxis: {
                        categories: [
                            {% for rp in country_list %}
                                {% for a in rp.list %}
                                    '{{ a.name}}',
                                {% endfor %}
                            {% endfor %}
                            ],
                        labels: {
                            rotation: -45,
                            align: 'right'
                        }
                    }, tooltip: {
                        formatter: function () {
                            return '<b>' + this.point.name + '</b>: ' + this.y;
                        }
                    }, exporting: {
                        enabled: false
                    }, plotOptions: {
                        column: {
                            dataLabels: {
                                enabled: false
                            }
                        }
                    }, legend: {
                        enabled: false
                    }, series: [
                        {
                            type: 'column',
                            data: [
                                {% for rp in resource_pool_vcpu %}
                                    {% for a in rp.list %}
                                        [ '{{ a.name}}', {{ a.vcpu_reserved }} ],
                                    {% endfor %}
                                {% endfor %}
                            ]
                        }
                    ]
                });
            });
        });

        {% regroup resource_pool|dictsort:"num_vm" by num_vm as resource_pool_vm %}
        $(function () {
            var chart;
            $(document).ready(function () {
                chart = new Highcharts.Chart({
                    credits: {
                        enabled: false
                    },
                    chart: {
                        renderTo: 'GuestColumnChart'
                    }, title: {
                        text: 'Number of Guests'
                    }, yAxis: {
                        title: {
                            text: 'vm'
                        }
	                 }, xAxis: {
                        categories: [
                            {% for rp in country_list %}
                                {% for a in rp.list %}
                                    '{{ a.name}}',
                                {% endfor %}
                            {% endfor %}
                            ],
                        labels: {
                            rotation: -45,
                            align: 'right'
                        }
                    }, tooltip: {
                        formatter: function () {
                            return '<b>' + this.point.name + '</b>: ' + this.y + ' vms';
                        }
                    }, exporting: {
                        enabled: false
                    }, plotOptions: {
                        column: {
                            dataLabels: {
                                enabled: false
                            }
                        }
                    }, legend: {
                        enabled: false
                    }, series: [
                        {
                            type: 'column',
                            data: [
                                {% for rp in resource_pool_vm %}
                                    {% for a in rp.list %}
                                        [ '{{ a.name}}', {{ a.num_vm }} ],
                                    {% endfor %}
                                {% endfor %}
                            ]
                        }
                    ]
                });
            });
        });
    </script>
{% endblock %}
